#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "mg_Lcd_Driver.h"
#include "mg_Device.h"
#include "mg_pwm.h"
#include "iic/mmp_iic.h"

Lcd_User_Action_Cmd Action_Cmd;
//毫秒级延时（非阻塞）
void Lcd_Spi_Delay_Ms(uint32_t ms)
{
	usleep(1000 * ms);
}
//背光初始化
void BackLight_Init(void)
{
	//ithPwmInit(BackLight_PWM, BackLight_PWM_Hz, BackLight_PWM_DUTY);
	//ithPwmReset(BackLight_PWM, BackLight_PWM_Pin, BackLight_GPIO_MODE);
	//BackLight_On();
}
//设置背光PWM占空比(0-100)
void BackLight_Duty_Set(uint8_t DutyPercent)
{
	if (DutyPercent>=100)
		ithPwmSetDutyCycle(CFG_GPIO_PWM_NUMBER - 1, 99);
	else
		ithPwmSetDutyCycle(CFG_GPIO_PWM_NUMBER - 1, DutyPercent);
}
//背光开
void BackLight_On(void)
{
	ithPwmEnable(BackLight_PWM, BackLight_PWM_Pin, BackLight_GPIO_MODE);
}
//背光关
void BackLight_Off(void)
{
	ithPwmDisable(BackLight_PWM, BackLight_PWM_Pin);
}
//LCD初始化
void BOARD_InitLcd(void)
{
	uint32_t tick1, tick2;
	tick1 = SDL_GetTicks();
	//初始化部分IO口
	_gpioPinInit();
	//SPI设置LCD寄存器
	BOARD_InitLCD_SPI();
	Dev.Lcd_Is_Sleep = false;
	tick2 = SDL_GetTicks();
	printf("Moorgen INFO:LCD Init Time: %dms\n", tick2 - tick1);
}
//初始化模拟SPI的IO口
static void _gpioPinInit(void)
{
	ithGpioSetOut(LCD_CS_GPIO);
	ithGpioSetMode(LCD_CS_GPIO, ITH_GPIO_MODE0);
	ithGpioSetOut(LCD_SDA_GPIO);
	ithGpioSetMode(LCD_SDA_GPIO, ITH_GPIO_MODE0);
	ithGpioSetOut(LCD_SCL_GPIO);
	ithGpioSetMode(LCD_SCL_GPIO, ITH_GPIO_MODE0);
	ithGpioSetOut(LCD_RST_GPIO);
	ithGpioSetMode(LCD_RST_GPIO, ITH_GPIO_MODE0);
	printf("Moorgen INFO:240*240_RGB565_LCD SPI GPIO Init completed\n");
}
#if 0//正扫
//初始化LCD寄存器
void BOARD_InitLCD_SPI(void)
{
	Set_LCD_SPI_RES();
	Lcd_Spi_Delay_Ms(5);
	Clr_LCD_SPI_RES();
	Lcd_Spi_Delay_Ms(20);
	Set_LCD_SPI_RES();
	Lcd_Spi_Delay_Ms(5);

	write_command(0xFE);
	write_command(0xEF);

	write_command(0xEB);
	write_data(0x14);
	write_command(0x84);
	write_data(0x65);
	write_command(0x85);
	write_data(0xFF);
	write_command(0x86);
	write_data(0xFF);
	write_command(0x87);
	write_data(0xFF);
	write_command(0x88);
	write_data(0x0A);
	write_command(0x89);
	write_data(0x21);
	write_command(0x8A);
	write_data(0x40);
	write_command(0x8B);
	write_data(0x80);
	write_command(0x8C);
	write_data(0x01);
	write_command(0x8D);
	write_data(0x01);
	write_command(0x8E);
	write_data(0xFF);
	write_command(0x8F);
	write_data(0xFF);

	write_command(0xB6);
	write_data(0x00);
	write_data(0x00);

	write_command(0x36);
	write_data(0x48);


	write_command(0x3a);//´«ʤ¸񊽑¡Ա
	write_data(0x55);///RGB MODE SELECTED

	write_command(0xf6);//½ӿڲΊýŤփ
	write_data(0xc6);//RGBmode-16/18bit

	write_command(0xb0);//RGBЅºŅ䖃
	write_data(0x42);////40:DE MODE   60:SYNC MODE

	write_command(0xb5);//ϻҾŤփ
	write_data(0x08);//vfp[7:0]      host  8
	write_data(0x09);//vbp[6:0]      host  4
	write_data(0x14);//hbp[4:0]      host 20


	write_command(0x90);
	write_data(0x08);
	write_data(0x08);
	write_data(0x08);
	write_data(0x08);

	write_command(0xBD);
	write_data(0x06);
	///////////add///////////////////////
	write_command(0xA6);
	write_data(0x74);

	write_command(0xBF);
	write_data(0x1C);

	write_command(0xA7);
	write_data(0x45);

	write_command(0xA9);
	write_data(0xBB);

	write_command(0xB8);
	write_data(0x63);
	/////////////////////////////////
	write_command(0xBC);
	write_data(0x00);

	write_command(0xFF);
	write_data(0x60);
	write_data(0x01);
	write_data(0x04);

	write_command(0xC3);
	write_data(0x21);
	write_command(0xC4);
	write_data(0x21);

	write_command(0xC9);
	write_data(0x25);

	write_command(0xBE);
	write_data(0x11);

	write_command(0xE1);
	write_data(0x10);
	write_data(0x0E);

	write_command(0xDF);
	write_data(0x21);
	write_data(0x0c);
	write_data(0x02);

	write_command(0xF0);
	write_data(0x45);
	write_data(0x09);
	write_data(0x08);
	write_data(0x08);
	write_data(0x26);
	write_data(0x2A);

	write_command(0xF1);
	write_data(0x43);
	write_data(0x70);
	write_data(0x72);
	write_data(0x36);
	write_data(0x37);
	write_data(0x6F);

	write_command(0xF2);
	write_data(0x45);
	write_data(0x09);
	write_data(0x08);
	write_data(0x08);
	write_data(0x26);
	write_data(0x2A);

	write_command(0xF3);
	write_data(0x43);
	write_data(0x70);
	write_data(0x72);
	write_data(0x36);
	write_data(0x37);
	write_data(0x6F);

	write_command(0xED);
	write_data(0x1B);
	write_data(0x0B);

	write_command(0xAE);
	write_data(0x77);

	write_command(0xCD);
	write_data(0x63);


	write_command(0x70);
	write_data(0x07);
	write_data(0x07);
	write_data(0x04);
	write_data(0x0E);
	write_data(0x0F);
	write_data(0x09);
	write_data(0x07);
	write_data(0x08);
	write_data(0x03);

	write_command(0xE8);
	write_data(0x24);
	/////////////////////////////////////////////
	write_command(0x60);
	write_data(0x38); //STV1
	write_data(0x0B);
	write_data(0x6D);
	write_data(0x6D);

	write_data(0x39);//STV2
	write_data(0xF0);
	write_data(0x6D);
	write_data(0x6D);


	write_command(0x61);
	write_data(0x38);//STV3
	write_data(0xF4);
	write_data(0x6D);
	write_data(0x6D);

	write_data(0x38);//STV4
	write_data(0xF7);
	write_data(0x6D);
	write_data(0x6D);
	/////////////////////////////////////
	write_command(0x62);
	write_data(0x38);
	write_data(0x0D);
	write_data(0x71);
	write_data(0xED);
	write_data(0x70);
	write_data(0x70);
	write_data(0x38);
	write_data(0x0F);
	write_data(0x71);
	write_data(0xEF);
	write_data(0x70);
	write_data(0x70);

	write_command(0x63);
	write_data(0x38);
	write_data(0x11);
	write_data(0x71);
	write_data(0xF1);
	write_data(0x70);
	write_data(0x70);
	write_data(0x38);
	write_data(0x13);
	write_data(0x71);
	write_data(0xF3);
	write_data(0x70);
	write_data(0x70);
	///////////////////////////////////////////////////////
	write_command(0x64);
	write_data(0x28);
	write_data(0x29);
	write_data(0xF1);
	write_data(0x01);
	write_data(0xF1);
	write_data(0x00);
	write_data(0x07);

	//սɨ
	write_command(0x66);
	write_data(0x3C);
	write_data(0x00);
	write_data(0xCD);
	write_data(0x67);
	write_data(0x45);
	write_data(0x45);
	write_data(0x10);
	write_data(0x00);
	write_data(0x00);
	write_data(0x00);

	write_command(0x67);
	write_data(0x00);
	write_data(0x3C);
	write_data(0x00);
	write_data(0x00);
	write_data(0x00);
	write_data(0x01);
	write_data(0x54);
	write_data(0x10);
	write_data(0x32);
	write_data(0x98);



	write_command(0x74);
	write_data(0x10);
	write_data(0x85);
	write_data(0x80);
	write_data(0x00);
	write_data(0x00);
	write_data(0x4E);
	write_data(0x00);

	write_command(0x98);
	write_data(0x3e);
	write_data(0x07);



	write_command(0x35);
	write_data(0x00);
	write_command(0x21);
	Lcd_Spi_Delay_Ms(120);
	//--------end gamma setting--------------//

	write_command(0x11);
	Lcd_Spi_Delay_Ms(120);
	write_command(0x29);
	Lcd_Spi_Delay_Ms(120);
	write_command(0x2C);
	Lcd_Spi_Delay_Ms(120);

}
#endif
#if 1//老屏/反扫
//初始化LCD寄存器
void BOARD_InitLCD_SPI(void)
{
	Set_LCD_SPI_RES();
	Lcd_Spi_Delay_Ms(5);
	Clr_LCD_SPI_RES();
	Lcd_Spi_Delay_Ms(20);
	Set_LCD_SPI_RES();
	Lcd_Spi_Delay_Ms(5);

	write_command(0xFE);
	write_command(0xEF);

	write_command(0xEB);
	write_data(0x14);
	write_command(0x84);
	write_data(0x65);
	write_command(0x85);
	write_data(0xF1);
	write_command(0x86);
	write_data(0x98);
	write_command(0x87);
	write_data(0x28);
	write_command(0x88);
	write_data(0x0A);
	write_command(0x89);
	write_data(0x21);
	write_command(0x8A);
	write_data(0x40);
	write_command(0x8B);
	write_data(0x80);
	write_command(0x8C);
	write_data(0x01);
	write_command(0x8D);
	write_data(0x03);//0x00
	write_command(0x8E);
	write_data(0xDF);
	write_command(0x8F);
	write_data(0x52);

	write_command(0xB6);//正扫/反扫配置
	write_data(0x00);
	write_data(0x40);//GS 

	write_command(0x36);//显示XY轴调整，正反色调整
	write_data(0x08);

	write_command(0xf6);//接口参数配置
	write_data(0xc6);//RGBmode-16/18bit

	write_command(0xb0);//RGB信号配置
	write_data(0x40);////40:DE MODE   60:SYNC MODE

	write_command(0x3A); //16/18bit配置
	write_data(0x55);

	write_command(0xb5);
	write_data(0x08);
	write_data(0x09);
	write_data(0x14);

	write_command(0x90);
	write_data(0x08);
	write_data(0x08);
	write_data(0x08);
	write_data(0x08);

	write_command(0xBD);
	write_data(0x06);

	//write_command(0xA6);
	//write_data(0x74);

	//write_command(0xBF);
	//write_data(0x1C);

	//write_command(0xA7);
	//write_data(0x45);

	write_command(0xA9);
	write_data(0xCC);

	//write_command(0xB8);
	//write_data(0x63);


	write_command(0xBC);
	write_data(0x00);

	write_command(0xFF);
	write_data(0x60);
	write_data(0x01);
	write_data(0x04);

	write_command(0xC3);///1a 1b		
	write_data(0x21);//0x17

	write_command(0xC4);///2a 2b		
	write_data(0x21);//0x17/

	write_command(0xC9);
	write_data(0x25);///vrg1a  2a 0x25

	write_command(0xBE);
	write_data(0x11);

	write_command(0xE1);
	write_data(0x10);
	write_data(0x0E);

	write_command(0xDF);
	write_data(0x21);
	write_data(0x0c);
	write_data(0x02);

	write_command(0xF0);
	write_data(0x45);
	write_data(0x09);
	write_data(0x08);
	write_data(0x08);
	write_data(0x26);
	write_data(0x2A);

	write_command(0xF1);
	write_data(0x43);
	write_data(0x70);
	write_data(0x72);
	write_data(0x36);
	write_data(0x37);
	write_data(0x6F);

	write_command(0xF2);
	write_data(0x45);
	write_data(0x09);
	write_data(0x08);
	write_data(0x08);
	write_data(0x26);
	write_data(0x2A);

	write_command(0xF3);
	write_data(0x43);
	write_data(0x70);
	write_data(0x72);
	write_data(0x36);
	write_data(0x37);
	write_data(0x6F);

	write_command(0xED);
	write_data(0x1B);
	write_data(0x0B);

	//write_command(0xAC);
	//write_data(0x47);
	write_command(0xAE);
	write_data(0x77);
	//write_command(0xCB);
	//write_data(0x02);
	write_command(0xCD);
	write_data(0x63);

	write_command(0x70);
	write_data(0x07);
	write_data(0x07);
	write_data(0x04);
	write_data(0x0E);
	write_data(0x0F);
	write_data(0x09);
	write_data(0x07);
	write_data(0x08);
	write_data(0x03);

	write_command(0xE8);
	write_data(0x24);

	write_command(0x60);
	write_data(0x38);
	write_data(0x0B);
	write_data(0x6D);
	write_data(0x6D);

	write_data(0x39);
	write_data(0xF0);
	write_data(0x6D);
	write_data(0x6D);


	write_command(0x61);
	write_data(0x38);
	write_data(0xF4);
	write_data(0x6D);
	write_data(0x6D);

	write_data(0x38);
	write_data(0xF7);
	write_data(0x6D);
	write_data(0x6D);
	/////////////////////////////////////
	write_command(0x62);
	write_data(0x38);
	write_data(0x0D);
	write_data(0x71);
	write_data(0xED);
	write_data(0x70);
	write_data(0x70);
	write_data(0x38);
	write_data(0x0F);
	write_data(0x71);
	write_data(0xEF);
	write_data(0x70);
	write_data(0x70);

	write_command(0x63);
	write_data(0x38);
	write_data(0x11);
	write_data(0x71);
	write_data(0xF1);
	write_data(0x70);
	write_data(0x70);
	write_data(0x38);
	write_data(0x13);
	write_data(0x71);
	write_data(0xF3);
	write_data(0x70);
	write_data(0x70);
	///////////////////////////////////////////////////////
	write_command(0x64);
	write_data(0x28);
	write_data(0x29);
	write_data(0xF1);
	write_data(0x01);
	write_data(0xF1);
	write_data(0x00);
	write_data(0x07);


	//·´ɨ
	write_command(0x66);
	write_data(0x3C);
	write_data(0x00);
	write_data(0x98);
	write_data(0x10);
	write_data(0x32);
	write_data(0x45);
	write_data(0x01);
	write_data(0x00);
	write_data(0x00);
	write_data(0x00);

	write_command(0x67);
	write_data(0x00);
	write_data(0x3C);
	write_data(0x00);
	write_data(0x00);
	write_data(0x00);
	write_data(0x10);
	write_data(0x54);
	write_data(0x67);
	write_data(0x45);
	write_data(0xcd);
	////////////////////////////////////////////////

	write_command(0x74);
	write_data(0x10);
	write_data(0x85);
	write_data(0x80);
	write_data(0x00);
	write_data(0x00);
	write_data(0x4E);
	write_data(0x00);

	write_command(0x98);
	write_data(0x3e);
	write_data(0x07);
	write_command(0x99);
	write_data(0x3e);
	write_data(0x07);

	write_command(0x35);//撕裂作用线
	write_command(0x21);//画面反色
	Lcd_Spi_Delay_Ms(5);

	write_command(0x11);//退出
	Lcd_Spi_Delay_Ms(5);
	write_command(0x29);
	write_command(0x2C);
}
#endif

//LCD睡眠
void BOARD_LCD_Sleep(void)
{
	write_command(0x66);
	write_data(0x00);
	write_data(0x00);
	write_data(0x11);
	write_data(0x11);
	write_data(0x11);
	write_data(0x11);
	write_data(0x11);
	write_data(0x11);
	write_data(0x11);
	write_data(0x11);

	write_command(0x67);
	write_data(0x00);
	write_data(0x00);
	write_data(0x11);
	write_data(0x11);
	write_data(0x11);
	write_data(0x11);
	write_data(0x11);
	write_data(0x11);
	write_data(0x11);
	write_data(0x11);

	Lcd_Spi_Delay_Ms(50); //Delay 120ms 
	write_command(0x28);
	write_command(0x10);

	printf("Moorgen INFO:LCD Sleep............\n");
}
//LCD_SPI写命令
void write_command(unsigned char y) // (uchar y,uchar x)
{
	unsigned char i;
	Clr_LCD_SPI_CS();
	Clr_LCD_SPI_SCL();
	Clr_LCD_SPI_SDA();
	Set_LCD_SPI_SCL();
	for (i = 0; i<8; i++)
	{
		Clr_LCD_SPI_SCL();
		if (y & 0x80)
		{
			Set_LCD_SPI_SDA();
		}
		else
		{
			Clr_LCD_SPI_SDA();
		}
		Set_LCD_SPI_SCL();
		y = y << 1;
	}
	Set_LCD_SPI_CS();
}
//LCD_SPI写数据
void write_data(unsigned char w) // (uchar w, uchar v)
{
	unsigned char i;
	Clr_LCD_SPI_CS();
	Clr_LCD_SPI_SCL();
	Set_LCD_SPI_SDA();
	Set_LCD_SPI_SCL();
	for (i = 0; i<8; i++)
	{
		Clr_LCD_SPI_SCL();
		if (w & 0x80)
		{
			Set_LCD_SPI_SDA();
		}
		else
		{
			Clr_LCD_SPI_SDA();
		}
		Set_LCD_SPI_SCL();
		w = w << 1;
	}
	Set_LCD_SPI_CS();
}
/**************************LCD应用层************************/

//清空用户操作
void Clear_User_Action(void)
{
	Action_Cmd.Action_Times = 0;			//操作次数
	Action_Cmd.User_Action = Null_Action;	//操作事件
}

//LCD进入睡眠
void APP_Lcd_Sleep(void)
{
	if (Dev.Lcd_Is_Sleep == true)return;
	Dev.Lcd_Is_Sleep = true;
	BackLight_Duty_Set(0);
	BOARD_LCD_Sleep();
}
//LCD恢复
void APP_Lcd_Resume(void)
{
	if (Dev.Lcd_Is_Sleep == false)return;
	Dev.Lcd_Is_Sleep = false;
	BOARD_InitLCD_SPI();
	BackLight_Duty_Set(Sys_Config.byte.LightPercent);
}